var FollowTableList = {// 医生随访表格初始化
    id: "FollowTableList",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

function _cellStyles(value, row, index) {
    if (row.revisitStatus == 2) {
        return {
            css: {
                "background-color": "#fff1d5",
                "overflow": "hidden",
                "white-space": "nowrap",
                "text-overflow": "ellipsis"
            }
        }
    } else {
        return {css: {"overflow": "hidden", "white-space": "nowrap", "text-overflow": "ellipsis"}}
    }
}

FollowTableList.initColumn = function () {//初始化表格的列
    var columns = [
        {field: 'selectItem', radio: true},
        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: 'customerId', field: 'customerId', visible: false, align: 'center', valign: 'middle'},
        {
            title: '诊所', field: 'arrivalStore', align: 'center', valign: 'middle', sortable: true, width: 90,
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '随访时间', field: 'revisitTime', align: 'center', valign: 'middle', sortable: true, width: 120,
            formatter: function (value, row, index) {
                return value.substring(0, 11);
            },
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '随访医生', field: 'doctor', align: 'center', valign: 'middle', sortable: true, width: 110,
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '随访人员', field: 'revisit', align: 'center', valign: 'middle', sortable: true, width: 110,
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '咨询师', field: 'fieldTc', align: 'center', valign: 'middle', sortable: true, width: 90,
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '随访状态', field: 'revisitStatus', align: 'center', valign: 'middle', sortable: true, width: 110,
            formatter: function (value, row, index) {
                if (value == 1) {
                    return "待计划";
                } else if (value == 2) {
                    return "<span>" + "回访完成" + "</span>"
                } else if (value == 3) {
                    return "回访未完成"
                } else if (value == 4) {
                    return "回访取消"
                }
            },
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '随访内容', field: 'remarks', align: 'center', valign: 'middle', sortable: true, width: 120,
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '随访结果', field: 'revisitContent', align: 'center', valign: 'middle', sortable: true, width: 110,
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '操作', field: '', align: 'center', valign: 'middle', sortable: true, width: 80,
            formatter: function (value, row, index) {
                var ids = row.id + "," + row.reservationsId + "," + row.customerId + "," + row.revisitTime;
                if (row.revisitStatus == 1) {
                    return '<button title="编辑随访" style="background: #ffffff;border: 1px solid #cccccc;padding: 1px 4px 0px 5px;" onclick="FollowTableList.followRevision(' + "'" + ids + "'" + ')" type="button" class="btn"><span class="glyphicon glyphicon-edit"></span></button> ' +
                        '<button title="删除随访" onclick="FollowTableList.deleteAppointment(\'' + row.id + '\',\'' + row.customerName + '\')" type="button" class="btn" style="background: #ffffff;border: 1px solid #cccccc;padding: 1px 4px 0px 5px;"><span class="glyphicon glyphicon-trash"></span></button>' +
                        '<button title="日志" onclick="FollowTableList.daily(\'' + row.reservationsId + '\')" type="button" class="btn" style="background: #ffffff;border: 1px solid #cccccc;padding: 1px 4px 0px 5px;"><span class="glyphicon glyphicon-th"></span></button>'
                } else {
                    return '<button title="查看随访" style="background: #ffffff; border: 1px solid #cccccc;padding: 1px 4px 0px 5px;" onclick="FollowTableList.followRevision(' + "'" + ids + "'" + ')" type="button" class="btn"><span class="glyphicon glyphicon-eye-open"></span></button> ' +
                        '<button title="删除随访" onclick="FollowTableList.deleteAppointment(\'' + row.id + '\',\'' + row.customerName + '\')" type="button" class="btn" style="background: #ffffff;border: 1px solid #cccccc;padding: 1px 4px 0px 5px;"><span class="glyphicon glyphicon-trash"></span></button>' +
                        '<button title="日志" onclick="FollowTableList.daily(\'' + row.reservationsId + '\')" type="button" class="btn" style="background: #ffffff;border: 1px solid #cccccc;padding: 1px 4px 0px 5px;"><span class="glyphicon glyphicon-th"></span></button>'
                }
            },
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
    ]
    return columns;
};

FollowTableList.check = function () {// 检查是否选中
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        FollowTableList.seItem = selected[0];
        return true;
    }
};

FollowTableList.followRevision = function (ids) {//查看随访
    var index = layer.open({
        type: 2,
        title: '随访',
        area: ['60%', '100%'], //宽高
        fix: true, //不固定
        maxmin: true,
        offset: ['0px', '40%'],
        content: Feng.ctxPath + '/doctorRevisit/look_doctor_follows/' + ids
    });
    this.layerIndex = index;
};

FollowTableList.daily = function (reservationsId) {//日志
    var index = layer.open({
        type: 2,
        title: '日志',
        area: ['80%', '80%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/doctorLooked/doctor_daily/' + reservationsId
    });
    this.layerIndex = index;
};

FollowTableList.deleteAppointment = function (id, customerName) {//删除随访
    if (confirm("患者【" + customerName + "】确认删除回访?")) {
        $.ajax({
            url: Feng.ctxPath + "/doctorLooked/delete_revisit",
            type: "post",
            data: {
                id: id
            },
            success: function (data) {
                if (data == -1) {
                    alert("亲，不能取消其他医生的随访！");
                    return;
                } else {
                    Feng.success('操作成功!');
                    window.FollowTableList.search();
                }
            },
            error: function (data) {
                alert('没有权限，请联系管理员!');
            }
        });
    }
};

FollowTableList.search = function () {
    FollowTableList.table.refresh();
    $('#FollowTableList').bootstrapTable('resetView', {height: $(window).height() - 105});
};

$(function () {
    var defaultColunms = FollowTableList.initColumn();
    var reservationsId = $("#reservationsId").val();
    var table = new BSTable(FollowTableList.id, "/doctorLooked/followTableList/" + reservationsId, defaultColunms);
    table.setPaginationType("client");//客户端分页
    FollowTableList.table = table.init();
    FollowTableList.search();
    $('#FollowTableList').bootstrapTable('resetView', {height: $(window).height() - 190});
    $.ajax({
        url: Feng.ctxPath + "/doctorDetails/doctorFollow",
        type: "post",
        data: {
            "reservationsId": document.getElementById("reservationsId").value
        },
        success: function (data) {
            var doctorselhtml = "";
            for (var i = 0; i < data.doctorInfoList.length; i++) {
                doctorselhtml += " <option value=\"" + data.doctorInfoList[i].doctor + "\">" + data.doctorInfoList[i].doctor + "</option>";
            }
            var html = "<div class=\"divBox\">" +
                "                    <div>" +
                "                        <h4>就诊</h4>" +
                "                        <p style=\"text-indent: 1em;margin: 0;\">处置</p>" +
                "                        <div class=\"clearfix\" style=\"height: 200px; overflow: auto\">";
            for (var i = 0; i < data.caseInfoList.length; i++) {
                html += "<div class=\"col-sm-12 clearfix\" style=\"border: 1px solid #cccccc;background:#F9F9F9;\">" +
                    "                                    <div class=\"col-sm-2\" style=\"padding: 0;margin-top: 25px;width: 90px;\">" + data.caseInfoList[i].operationTime + "</div>";
                for (var j = 0; j < data.caseInfoList[i].toothSite5List.length; j++) {
                    html += "<div class=\"col-sm-3\" style=\"margin-top: 11px;\">" +
                        "                                        <div style=\"width: 100%;height:50px;display: flex;flex-wrap:wrap;\">" +
                        "                                            <input class=\"tool_ipt\" style=\"border-right: 1px solid black;border-bottom: 1px solid black;text-align: right;padding-right: 4px\" id=\"leftUp5\"  value=\"" + data.caseInfoList[i].toothSite5List[j].leftUp + "\" readonly=\"readonly\">" +
                        "                                            <input class=\"tool_ipt\" style=\"border-bottom: 1px solid black;padding-left: 4px\" id=\"rightUp5\"  value=\"" + data.caseInfoList[i].toothSite5List[j].rightUp + "\" readonly=\"readonly\">" +
                        "                                            <input class=\"tool_ipt\" style=\"border-right: 1px solid black;text-align: right;padding-right: 4px\" id=\"leftDown5\"  value=\"" + data.caseInfoList[i].toothSite5List[j].leftDown + "\" readonly=\"readonly\">" +
                        "                                            <input class=\"tool_ipt\" style=\"padding-left: 4px\" id=\"rightDown\"  value=\"" + data.caseInfoList[i].toothSite5List[j].rightDown + "\" readonly=\"readonly\">" +
                        "                                        </div>" +
                        "                                    </div>" +
                        "                                    <div class=\"col-sm-7\" style=\"border-left: 1px solid #cccccc;padding-right: 0;\">" +
                        "                                        <div style=\"padding: 11px 0 8px;\"><textarea disabled=\"disabled\" style=\"width: 100%;height: 50px;\">" + data.caseInfoList[i].toothSite5List[j].remarks + "</textarea></div>" +
                        "                                    </div>";
                }
                html += "</div>";
            }
            html += "</div>" +
                "                        <div>" +
                "                            <p style=\"text-indent: 1em;margin-top: 10px;\">医嘱</p>" +
                "                            <div style=\"height: 300px;overflow: auto\">";
            for (var i = 0; i < data.caseInfoList.length; i++) {
                html += "<div style=\"display: flex;align-items: center;padding: 5px 10px;margin-bottom: 5px;border: 1px solid #cccccc;background: #F9F9F9;\">" +
                    "                                    <span style=\"width: 90px;\">" + data.caseInfoList[i].operationTime + "</span>" +
                    "                                    <textarea disabled=\"disabled\" rows=\"2\" style=\";margin-left: 1%;flex: 1;width: 100%\">" + data.caseInfoList[i].doctorAdvice + "</textarea>" +
                    "                                </div>";
            }
            html += "</div>" +
                "                        </div>" +
                "                    </div>" +
                "                    <div>" +
                "                        <h4>随访</h4><ul class=\"ulStyle clearfix\">" +
                "                            <li class=\"col-sm-12\">" +
                "                                <span class=\"col-sm-3 \">随访时间：</span>" +
                "                                <input id=\"revisitTime\" class=\"inpStyle col-sm-9\" autocomplete=\"off\" type=\"text\" onclick=\"laydate({istiem:true,format:'YYYY-MM-DD'})\">" +
                "                            </li>" +
                "                            <li class=\"col-sm-12\">" +
                "                                <span class=\"col-sm-3\" >随访医生：</span>" +
                "                                <select id=\"doctorsf\" class=\"inpStyle col-sm-9\" name=\"doctorsf\" >" +
                "                                    <option value=\"\">--请选择医生--</option>";
            html += doctorselhtml;
            html += "</select>" +
                "                            </li>" +
                "                            <li class=\"col-sm-12\">" +
                "                                <span class=\"col-sm-3\" >随访人员：</span>" +
                "                                <select id=\"revisit\" class=\"inpStyle col-sm-9\" name=\"revisit\" >" +
                "                                    <option value=\"\">--请选择医生--</option>";
            html += doctorselhtml;
            html += " </select>" +
                "                           </li>" +
                "                            <li class=\"col-sm-12\">" +
                "                                <span class=\"col-sm-3\">创建人：</span>" +
                "                                <input class=\"inpStyle col-sm-9\" id=\"createName\" name=\"inpStyle\" disabled=\"disabled\" value=\"" + data.userName + "\">" +
                "                            </li>" +
                "                            <li class=\"col-sm-12\">" +
                "                                <span class=\"col-sm-3\">内容模板：</span>" +
                "                                <div class=\"btn-group\" style=\"position: relative\">" +
                "                                    <button type=\"button\" class=\"btn\" onclick=\"addsf()\" style=\"position: absolute;left: 100px;top: 0;background: white\">添加模板</button>" +
                "                                    <button class=\"btn btn-default dropdown-toggle\" type=\"button\" id=\"dropdownMenu2\" data-toggle=\"dropdown\" aria-haspopup=\"true\" aria-expanded=\"false\">" +
                "                                        内容模板<span class=\"caret\"></span></button>" +
                "                                    <ul class=\"dropdown-menu\" aria-labelledby=\"dropdownMenu2\" style=\"left: 0;\" id=\"dropdownMenu2Ul\"></ul>" +
                "                                </div>" +
                "                            </li>" +
                "                            <li class=\"col-sm-12\">" +
                "                                <span class=\"col-sm-3\">随访内容：</span>" +
                "                                <textarea id=\"remarks2\" class=\"col-sm-9\" name=\"remarks2\" style=\"height: 60px;width: 60%;padding: 0 5px;\"></textarea>" +
                "                            </li>" +
                "                            <li class=\"col-sm-12\">" +
                "                                <span class=\"col-sm-3\">结果模板：</span>" +
                "                                <div class=\"btn-group\" style=\"position: relative\">" +
                "                                    <button type=\"button\" class=\"btn\" onclick=\"addjg()\" style=\"position: absolute;left: 100px;top: 0;background: white\">添加模板</button>" +
                "                                    <button class=\"btn btn-default dropdown-toggle\" type=\"button\" id=\"dropdownMenu1\" data-toggle=\"dropdown\" aria-haspopup=\"true\" aria-expanded=\"false\">" +
                "                                        结果模板<span class=\"caret\"></span></button>" +
                "                                    <ul class=\"dropdown-menu\" aria-labelledby=\"dropdownMenu1\" style=\"left: 0;\" id=\"dropdownMenu1Ul\"></ul>" +
                "                                </div>" +
                "                            </li>" +
                "                            <li class=\"col-sm-12\">" +
                "                                <span class=\"col-sm-3\">随访结果：</span>" +
                "                                <textarea id=\"revisitContent\" class=\"col-sm-9\" name=\"revisitContent\" style=\"height: 60px;width: 60%;padding: 0 5px;\"></textarea>" +
                "                            </li>" +
                "                        </ul>" +
                "                                <div class=\"btnBox\">" +
                "                                    <button class=\"btn\" type=\"button\" onclick='submitDoctro1()'>保存</button>" +
                "                                    <button class=\"btn\" type=\"button\" onclick='submitDoctro2()'>再随访</button>" +
                "                                    <button class=\"btn\" type=\"button\" onclick='submitDoctro3()'>随访完成</button>" +
                "                                    <button class=\"btn\" type=\"button\" onclick='submitDoctro4()'>随访未完成</button>" +
                "                                    <button class=\"btn\" type=\"button\" onclick='submitDoctro5()'>取消本随访</button>" +
                "                                </div>" +
                "                    </div>" +
                "</div>";
            $("#newfollow").html(html);
            var seleteDoctorValue = $('#doctorValue').val();
            if (seleteDoctorValue != "") {
                $("#doctorsf option[value='" + seleteDoctorValue + "']").attr("selected", "selected");
                $("#revisit option[value='" + seleteDoctorValue + "']").attr("selected", "selected");
            }
            var sfList = "";
            for (var i = 0; i < data.sfList.length; i++) {
                sfList += '<li id="' + data.sfList[i].id + '" style="position: relative"><a href="javascript:;">' + data.sfList[i].title + '</a><span class="glyphicon glyphicon-remove" style="position: absolute;top: 50%;right: 10px;transform: translateY(-50%)"></span></li>'
            }
            var jgList = "";
            for (var i = 0; i < data.jgList.length; i++) {
                jgList += '<li id="' + data.jgList[i].id + '" style="position: relative"><a href="javascript:;">' + data.jgList[i].title + '</a><span class="glyphicon glyphicon-remove" style="position: absolute;top: 50%;right: 10px;transform: translateY(-50%)"></span></li>'
            }
            $("#dropdownMenu2Ul").html(sfList);
            $("#dropdownMenu1Ul").html(jgList);
            $("#dropdownMenu2Ul").on('click', 'li', function () {
                var value = $(this).attr('id')
                $.ajax({
                    url: Feng.ctxPath + "/doctorLooked/getSfById",
                    type: "post",
                    data: {
                        id: value
                    },
                    success: function (data) {
                        $("#remarks2").val(data.remarks)
                        $("#remarks").val(data.remarks)
                    },
                    error: function (data) {
                        alert('没有权限，请联系管理员!');
                    }
                });
            });
            $("#dropdownMenu1Ul").on('click', 'li', function () {
                var value = $(this).attr('id');
                $.ajax({
                    url: Feng.ctxPath + "/doctorLooked/getJgById",
                    type: "post",
                    data: {
                        id: value
                    },
                    success: function (data) {
                        $("#revisitContent").val(data.remarks)
                    },
                    error: function (data) {
                        alert('没有权限，请联系管理员!');
                    }
                });
            });
            $("#dropdownMenu2Ul").on('click', '.glyphicon-remove', function (e) {
                e.stopPropagation();
                var value = $(this).parent().attr('id');
                if (confirm("是否删除此模板？")) {
                    $.ajax({
                        url: Feng.ctxPath + "/doctorLooked/deleteSfById",
                        type: "post",
                        data: {
                            id: value
                        },
                        success: function (data) {
                            Feng.success("删除成功!");
                            window.location.reload();
                        },
                        error: function (data) {
                        }
                    });
                }
            });
            $("#dropdownMenu1Ul").on('click', '.glyphicon-remove', function (e) {
                e.stopPropagation();
                var value = $(this).parent().attr('id');
                if (confirm("是否删除此模板？")) {
                    $.ajax({
                        url: Feng.ctxPath + "/doctorLooked/deleteJgById",
                        type: "post",
                        data: {
                            id: value
                        },
                        success: function (data) {
                            Feng.success("删除成功!");
                            window.location.reload();
                        },
                        error: function (data) {
                        }
                    });
                }
            });
            var time = new Date();
            var y = time.getFullYear();
            var m = time.getMonth() + 1;
            var d = time.getDate();
            if (m < 10) {
                m = "0" + m
            }
            if (d < 10) {
                d = "0" + d
            }
            $("#revisitTime").val(y + "-" + m + "-" + d)
        },
        error: function (data) {
            alert('没有权限，请联系系统管理员!');
            parent.layer.close(parent.layer.getFrameIndex(window.name));
        }
    });
});